package com.jr.controller;

import com.google.gson.Gson;
import com.jr.entry.Enterprise;
import com.jr.entry.User;

import com.jr.service.UserService;
import com.jr.service.impl.EnterpriseServiceImpl;
import com.jr.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

@WebServlet("/UserController/*")
public class UserController extends HttpServlet {
    UserService userService=new UserServiceImpl();
    Gson gson=new Gson();
    EnterpriseServiceImpl enterpriseimpl=new EnterpriseServiceImpl();
    Enterprise enterprise=new Enterprise();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");
        String url = request.getRequestURI();
        String methodName = url.substring(url.lastIndexOf("/") + 1);
        Class cla = UserController.class;
        try {
            Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(cla.newInstance(), request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

    }
   public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
       String e = request.getParameter("e");
       String p = request.getParameter("p");
       boolean a = userService.login(e, p);
       response.getWriter().println(gson.toJson(a));
   }

   public void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
           String account = request.getParameter("account");
           String name = request.getParameter("name");
           String idcardNo = request.getParameter("idcardNo");
           String enterpriseName = request.getParameter("enName");
           boolean b = userService.updateUser(name, idcardNo,account,enterpriseName)&userService.updateUserEnterpriseIdAndName(account,enterpriseimpl.selectEnterpriseByName(enterpriseName).getId().toString(),enterpriseName);
           response.getWriter().println(gson.toJson(b));

   }
    public void selectUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String account = request.getParameter("account");
        User user = userService.selectUserByAccount(account);
        response.getWriter().println(gson.toJson(user));
    }


    public void addEnter(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String codeCode = request.getParameter("CodeCode");
        String number = request.getParameter("number");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String address =request.getParameter("address");
        String fax = request.getParameter("fax");
        enterprise.setName(name);
        enterprise.setSocialUniformCode(codeCode);
        enterprise.setEmail(email);
        enterprise.setPhone(phone);
        enterprise.setAddress(address);
        enterprise.setFax(fax);
        enterprise.setScale(number);
            boolean b = enterpriseimpl.insertEnterprise(this.enterprise);
            response.getWriter().println(gson.toJson(b));




    }
    public void selectenter(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("enterName");
        String account = request.getParameter("account");
        Enterprise enterprise;
        if(name!=null) {
            enterprise = enterpriseimpl.selectEnterpriseByName(name);
        } else {
            enterprise = enterpriseimpl.selectEnterpriseByAccount(account);
        }
        response.getWriter().println(gson.toJson(enterprise));

    }
    public void updateuserid(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        String name1 = request.getParameter("name1");
        String account = request.getParameter("account");
        boolean b = userService.updateUserEnterpriseIdAndName(account, id, name1);
        response.getWriter().println(gson.toJson(b));
    }

}
